/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Magnetometer;
import java.io.*;
import java.net.*;
import java.util.*;

/**
 *
 * @author Matt Koehler
 */




public class MagnetometerMainGUI extends javax.swing.JFrame implements EventListener {
    
    private String Time;
    //private String[] VolitileData;
    private double a,b,c,d,XTotal,YTotal,ZTotal,i,MagneticStrength,MaxX,MinX,KpRange;
    private ArrayList<DataPoint> MagnetometerData = new ArrayList<>();
    //*private URL url;
    private DataPoint Data=new DataPoint();

    
    
    /**
     * Creates new form MagnetometerMainGUI
     */
    public MagnetometerMainGUI() {
        initComponents();
           i=0;
        XTotal=0;
        YTotal=0;
        ZTotal=0;
        MaxX=1;
        MinX=100000;
        String[] VolitileData = new String[5];

    }
    
   public void ProcessData() throws IOException {
   try{ //String DataDate = "http://themis.ssl.berkeley.edu/EPO_Access/files/mcgr/2012/thg_l2_mag_mcgr_20120315_v01.txt";
   URL url = new URL("http://themis.ssl.berkeley.edu/EPO_Access/files/swno/2010/thg_l2_mag_swno_20100101_v01.txt");
       //(BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) 
       Scanner scan= new Scanner(url.openStream());    
           String inputLine=scan.next();
           
           while ((inputLine) != null) {
               // Process each line.
               String phrase = inputLine;
               String delims = "[ ]+";
               String[] VolitileData = phrase.split(delims);
          
               Time=VolitileData[1];
               b=Double.parseDouble(VolitileData[2]);
               c=Double.parseDouble(VolitileData[3]);
               d=Double.parseDouble(VolitileData[4]);
           if (b > MaxX) {
               MaxX=b;
           }
           if (b < MinX) {
               MinX=b;
           }
               Data.setTime(Time);
               Data.setX(b);
               Data.setY(c);
               Data.setZ(d);
               i++;
               XTotal=XTotal+b;
               YTotal=YTotal+c;
               ZTotal=ZTotal+d;
               MagnetometerData.add(Data);
           
          }   
           //System.out.println(MagnetometerData.size());
            MagneticStrength=Math.sqrt(Math.pow(XTotal/i,2)+Math.pow(YTotal/i,2)+Math.pow(ZTotal/i,2));
            KpRange=MaxX-MinX;
            //System.out.println(MagneticStrength);
            //System.out.println(KpRange);
            //System.out.println(MaxX);
            //System.out.println(MinX);
       }
  
   
   catch (IOException | NumberFormatException error){
       System.out.println("Error 1");
   }
   }


  


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        MagStrOutput = new javax.swing.JTextField();
        Calculate = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        MagStrOutput.setEditable(false);
        MagStrOutput.setText("OUTPUT");

        Calculate.setText("Calulate");
        Calculate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CalculateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(184, 184, 184)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Calculate)
                    .addComponent(MagStrOutput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(332, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(104, 104, 104)
                .addComponent(Calculate)
                .addGap(18, 18, 18)
                .addComponent(MagStrOutput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(386, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void CalculateActionPerformed(java.awt.event.ActionEvent evt) {                                          
       // try{ 
            ProcessData();
       // }
        //catch (Exception error){
           //System.out.println("Error 2"); 
       // }
        
    }                                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MagnetometerMainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MagnetometerMainGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton Calculate;
    private javax.swing.JTextField MagStrOutput;
    // End of variables declaration                   
}
